package com.zjsru.oneDay202206;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/7/20
 * 二维网格迁移
 *
 * 输入：grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
 * 输出：[[9,1,2],[3,4,5],[6,7,8]]
 *
 */
public class shiftGrid {
    public List<List<Integer>> shiftGrid(int[][] grid, int k) {
        int n = grid.length, m = grid[0].length;
        int[][] mat = new int[n][m];
        for (int i = 0; i < m; i++) {
            int tcol = (i + k)%m, trow = ((i + k)/m)%n, idx = 0;
            while(idx != n) mat[(trow++) % n][tcol] = grid[idx++][i];
        }
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < m; j++) {
                list.add(mat[i][j]);
            }
            ans.add(list);
        }
        return ans ;
    }
    
    public static void main(String[] args) {
        shiftGrid shiftGrid = new shiftGrid();
        int[][] grid = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        int k = 1;
        System.out.println(shiftGrid.shiftGrid(grid, k));
    }
}
